package selectionweb.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import selectionweb.web.Student;

import javax.validation.Valid;
import java.io.*;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/personalInformation")
public class PersonalInformationController {
    private static String readJson(String path) {
        //从指定位置读取文件
        File file = new File(path);

        BufferedReader br = null;

        //返回值，使用StringBuffer
        StringBuffer data = new StringBuffer();
        try {
            br = new BufferedReader(new FileReader(file));
            //每次读取文件的缓存
            String temp = null;
            while ((temp = br.readLine()) != null) {
                data.append(temp);
                System.out.println(data);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("读取成功！");
        return data.toString();
    }

    /**
     * 向文件中写入数据
     * @param path 文件路径
     * @param json  json数据
     * @param fileName 文件名称
     */
    private static void writeJson(String path, Object json, String fileName) {
        BufferedWriter bw = null;
        File file = new File(path + fileName);

        //如果文件不存在，则新建一个
        if (! file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //写入数据
        try {
            bw = new BufferedWriter(new FileWriter(file));
            bw.write(json.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("写入成功！");
    }
    @GetMapping
    public String personalInformation(Model model) {
        model.addAttribute("student", new Student());
        return "personalInformation";
    }
    @PostMapping
        public String processOrder  (@Valid Student student,Errors errors)
    {
            if (errors.hasErrors()) {
                return "personalInformation";
            }

            log.info("Order submitted: " + student);
            writeJson("D://",student,"Student.txt");
            return "redirect:/selection";
    }
}
